#include "queue.h"
#include <stdlib.h>

void queue_init(Queue* queue) {
    queue->front = NULL;
    queue->rear = NULL;
    queue->size = 0;
}

void queue_enqueue(Queue* queue, int row, int col) {
    QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
    if (newNode == NULL) {
        return;
    }
    
    newNode->row = row;
    newNode->col = col;
    newNode->next = NULL;
    
    if (queue->rear == NULL) {
        queue->front = newNode;
        queue->rear = newNode;
    } else {
        queue->rear->next = newNode;
        queue->rear = newNode;
    }
    
    queue->size++;
}

int queue_dequeue(Queue* queue, int* row, int* col) {
    if (queue_is_empty(queue)) {
        return 0;
    }
    
    QueueNode* temp = queue->front;
    *row = temp->row;
    *col = temp->col;
    
    queue->front = queue->front->next;
    if (queue->front == NULL) {
        queue->rear = NULL;
    }
    
    free(temp);
    queue->size--;
    return 1;
}

int queue_is_empty(Queue* queue) {
    return queue->front == NULL;
}

void queue_free(Queue* queue) {
    while (!queue_is_empty(queue)) {
        int row, col;
        queue_dequeue(queue, &row, &col);
    }
} 